package com.sdyeda.readiot.scheduled;

import java.util.Date;
import java.util.concurrent.ScheduledFuture;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.scheduling.support.PeriodicTrigger;
import org.springframework.stereotype.Component;

import com.sdyeda.readiot.Read;
import com.sdyeda.readiot.config.Setting;
import com.sdyeda.readiot.scheduled.ScheduledInfo.ScheduledStatus;

/**
 * @author defly 任务控制类 提供Start、Stop、Restart、getStatus 等外部方法
 */
@Component
@Configuration
public class ScheduledControl {

	@Autowired
	private ThreadPoolTaskScheduler threadPoolTaskScheduler; // 注入线程池任务调度类

	// 任务延时：延时类型、延时，从配置文件读取
	// 使用配置文件可能导致未赋值
	// @Value("${com.sdyeda.readiot.scheduled-type}")
	private String type ;

	// @Value("${com.sdyeda.readiot.scheduled-cron}")
	private String cron;

	// @Value("${com.sdyeda.readiot.scheduled-delay}")
	private long delay ;

	// 任务信息，多个任务可用数组
	private static ScheduledInfo info = new ScheduledInfo();

	// 任务名
	private final String scheduledname = "Read Iot";

	// 任务延时：从配置文件读取

	// 初始化方法，程序运行自动启动任务
	//@Bean
	void Init() {
		
		//info = 
		info = new ScheduledInfo();
		
		type = (String) Setting.Get(Setting.SCHEDULED_TYPE);
		cron = (String) Setting.Get(Setting.SCHEDULED_CRON);
		delay = (Long) Setting.Get(Setting.SCHEDULED_DELAY);

		Read read = new Read();
		

		ScheduledFuture<?> schedule;
		if (type.equals("delay")) {
			//System.out.println("delay:" + delay);
			schedule = threadPoolTaskScheduler.schedule(read, new PeriodicTrigger(delay));
		} else {
			//System.out.println("cron:" + cron);
			schedule = threadPoolTaskScheduler.schedule(read, new CronTrigger(cron));
		}

		ScheduledFutureHolder scheduledFutureHolder = new ScheduledFutureHolder();
		scheduledFutureHolder.setScheduledFuture(schedule);
		scheduledFutureHolder.setRunnableClass(read.getClass());
		if (type.equals("delay")) {
			scheduledFutureHolder.setDelay(delay);
		} else {
			scheduledFutureHolder.setCron(cron);
		}
		scheduledFutureHolder.setType(type);

		info.setInfo(scheduledname, scheduledFutureHolder, new Date(), ScheduledStatus.running);
		System.out.println(ScheduledStatus.running);

	}

	public ScheduledInfo getStatus() {
		return info;
	}

	public ScheduledInfo Stop() {

		ScheduledFuture<?> schedule = info.getFutureholder().getScheduledFuture();
		if (info.getStatus() == ScheduledStatus.running) {
			schedule.cancel(true);

			info.setStatus(ScheduledStatus.stoped);
		}
		return info;
	}

	public ScheduledInfo Start() {
		// ScheduledFuture<?> schedule = info.getFutureholder().getScheduledFuture();
		if (info.getStatus() != ScheduledStatus.running) {
			
			Init();
			// schedule.

		}
		return info;
	}

	public ScheduledInfo Restart() {

		if (info.getStatus() == ScheduledStatus.running) {
			Stop();
		}
		Start();
		return info;
	}

}
